package com.fubowen.reader.vm

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.fubowen.reader.db.model.PermissionState
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.launch

class PermissionViewModel(application: Application) : AndroidViewModel(application) {

    private val mPermissionFlow = MutableSharedFlow<PermissionState>()
    val permissionFlow = mPermissionFlow.asSharedFlow()
    fun changePermission(state: PermissionState) = viewModelScope.launch {
        mPermissionFlow.emit(state)
    }

    fun changePermission(permission: String, state: Int) {
        PermissionState(
            permission = permission,
            state = state
        ).apply {
            changePermission(this)
        }
    }
}
